package com.xxx.base.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {

    @Value("${sys.swagger.enable:true}")
    private boolean enableSwagger;

    @Bean
    public Docket doctet(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .build()
                .directModelSubstitute(org.threeten.bp.LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(org.threeten.bp.OffsetDateTime.class, java.util.Date.class)
                .apiInfo(apiInfo())
                .enableUrlTemplating(true)
                .enable(enableSwagger);
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Interface Api Document")
                .description("This is a api document for Android interface.")
                .license("")
                .licenseUrl("http://unlicense.org")
                .termsOfServiceUrl("")
                .version("1.0.0")
                .contact(new Contact("","", "516892342@qq.com"))
                .build();
    }


    @Controller
    @ApiIgnore
    class doc {
        @RequestMapping("api-doc")
        public String index() {
            return "redirect:swagger-ui.html";
        }
    }


}
